/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ret;
        stack<TreeNode*> st;
        if(root==nullptr) return ret;
        st.push(root);
        while(!st.empty())
        {
            TreeNode* node=st.top();
            st.pop();
            ret.push_back(node->val);               //中
            if(node->left) st.push(node->left);     //左
            if(node->right) st.push(node->right);   //右
        }
        //则出栈顺序为 右->左->中   反转一下则是 中->左->右
        reverse(ret.begin(),ret.end());
        return ret;
    }
};